package cn.liye.pingfen.Util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@Component
public class SendSMSUtil {
    /**
     * http请求
     * @param url 请求路径
     * @param type 请求方式(GET/POST)
     * @param data 接口上送数据
     * @param encode 编码格式(UTF-8)
     * @return 字符串
     * @throws IOException
     */
    public static String sendHttp(String url, String type, String data, String encode) throws IOException {
        String result;
        HttpURLConnection httpConn = null;
        OutputStream o = null;
        InputStreamReader in = null;
        BufferedReader rd = null;
        try {
            URL someUrl = new URL(url);
            URLConnection conn = someUrl.openConnection();
            httpConn = (HttpURLConnection) conn;
            httpConn.setRequestMethod(type);
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.setUseCaches(false);
            httpConn.setRequestProperty("Content-type", "application/json; charset=" + encode);
            o = httpConn.getOutputStream();
            if (data.length() > 0) {
                o.write(data.getBytes(encode));
                o.flush();
            }
            int status = httpConn.getResponseCode();
            if (status != HttpURLConnection.HTTP_OK) {
                log.error("UnicomRechargeServiceImpl.sendHttp_0001_m:{},url:{},responseMessage:{}", type, url, httpConn.getResponseMessage());
                return null;
            } else {
                in = new InputStreamReader(httpConn.getInputStream(), encode);
                rd = new BufferedReader(in);
                StringBuffer buff = new StringBuffer();
                int c;
                while ((c = rd.read()) != -1) {
                    buff.append((char) c);
                }
                result = buff.toString();
                log.error("UnicomRechargeServiceImpl.sendHttp_0002_result:{}", result);
            }
        } catch (Exception e) {
            log.error("UnicomRechargeServiceImpl.sendHttp_0003_m:{},url:{},e:", type, url, e);
            return null;
        } finally {
            if (null != httpConn) {
                httpConn.disconnect();
            }
            if (null != rd) {
                rd.close();
            }
            if (null != in) {
                in.close();
            }
            if (null != o) {
                o.close();
            }
        }
        return result;
    }

    public static void main(String[] args) {

        // 创建一个Random对象
        Random random = new Random();

        // 生成一个1000到9999之间的随机整数
        int verificationCode = random.nextInt(9000) + 1000;

        // 打印验证码
        System.out.println("生成的验证码是: " + verificationCode);

        List<String> phoneList = new ArrayList<>();
        phoneList.add("18617951691");
        String 张三 = DigestUtils.md5Hex("张三");
        System.out.println(张三);
        String userName = "石家庄立业软件";
        String passWord = "KyJmQx6QEGDv";
        String content = "【企业评分网】您的验证码："+verificationCode+"该验证码5分钟内有效，请勿泄露于他人"; // 验证码
        long timeStamp= System.currentTimeMillis();
        // MD5(userName + content + timestamp + MD5(password))
        String sign = DigestUtils.md5Hex(userName + content + timeStamp+ DigestUtils.md5Hex(passWord).toLowerCase());
        String url = "http://api.shantuoke.com:8001/sms/api/sendMessage";

        JSONObject json = new JSONObject();
        json.put("userName", userName);
        json.put("content", content);
        json.put("timestamp", timeStamp);
        json.put("phoneList", phoneList);
        json.put("sign", sign);
        try {
            String post = SendSMSUtil.sendHttp(url, "POST", json.toString(), "UTF-8");
            System.out.println(json.toString());
            System.out.println(post);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
